package com.eqdd.yiqidian.ui.select;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.ViewPagerStripCustom;
import com.eqdd.yiqidian.adapter.MyFragmentPagerAdapter;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.SelectBean;
import com.eqdd.yiqidian.bean.rebuild.CareerListResultBean;
import com.eqdd.yiqidian.bean.rebuild.DepartmentListResultBean;
import com.eqdd.yiqidian.bean.rebuild.ShareConf;
import com.eqdd.yiqidian.ui.share.ShareWorkMateFragment;
import com.eqdd.yiqidian.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class SelectPostMultiActivity extends BaseActivity {
    private ViewPagerStripCustom dataBinding;
    private List<String> title = new ArrayList<>();
    private List<Fragment> views = new ArrayList<>();
    private List<String> ids = new ArrayList<>();
    public static ArrayList<CareerListResultBean> selectedBeans = new ArrayList<>();
    public static int totalNum = 0;
    private MyFragmentPagerAdapter myFragmentPagerAdapter;


    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_viewpager_strip);
        initTopTitleBar(View.VISIBLE, "岗位选择");
        initTopRightText("完成", v -> {
            if (selectedBeans.size() > 0) {
                finishAndResult();
            } else {
                ToastUtil.showShort("请选择岗位");
            }
        });
    }


    private void finishAndResult() {
        Intent intent = new Intent();
        intent.putParcelableArrayListExtra(Config.SELECTED_BEANS, selectedBeans);
        intent.putExtra(Config.NUM, totalNum);
        setResult(Config.SUCCESS, intent);
        finish();
    }

    @Override
    public void initData() {
        selectedBeans.clear();
        myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), title, views);
        dataBinding.viewPager.setAdapter(myFragmentPagerAdapter);
        dataBinding.slidingTabStrip.setViewPager(dataBinding.viewPager);
    }

    @Override
    public void setView() {
        DepartmentListResultBean departmentListResultBean = new DepartmentListResultBean();
        departmentListResultBean.setCompanyId(user.getCompanyId() + "");
        departmentListResultBean.setDepartId("0");
        departmentListResultBean.setParentId("0");
        departmentListResultBean.setDepartName(user.getCompany());
        addFragment(departmentListResultBean);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {


        }
    }

    public void addFragment(DepartmentListResultBean departmentListResultBean) {
        for (int i = 0; i < ids.size(); i++) {
            if (departmentListResultBean.getParentId().equals(ids.get(i))) {
                int len = ids.size();
                for (int j = len - 1; j >= i + 1; j--) {
                    title.remove(j);
                    views.remove(j);
                    ids.remove(j);
                }
                break;
            }
        }
        title.add(departmentListResultBean.getDepartName());
        SelectPostMultiFragment fragment = new SelectPostMultiFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(Config.DEPARTMENT, departmentListResultBean);
        fragment.setArguments(bundle);
        views.add(fragment);
        ids.add(departmentListResultBean.getDepartId() + "");
        myFragmentPagerAdapter.notifyDataSetChanged();
        dataBinding.viewPager.setCurrentItem(views.size() - 1);
    }

    @Override
    protected void onDestroy() {
        selectedBeans.clear();
        totalNum = 0;
        super.onDestroy();
    }
}
